package com.sitr.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @Description TODO
 * @Author Sitr
 * @Date 2021/3/1 9:59
 * @Version 1.0
 **/


public class WSServerInitialzer extends ChannelInitializer<SocketChannel> {
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast(new HttpServerCodec());

        pipeline.addLast(new ChunkedWriteHandler());

        pipeline.addLast(new HttpObjectAggregator(1024*64));


        //webSocket服务器处理的协议 用于指定给客户端连接访问的路由
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        //自定义的handdler
        System.out.println("ChatHandler 添加完成");
        pipeline.addLast(new ChatHandler());
    }
}
